/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.flink;

import org.apache.flink.api.java.DataSet;
import org.apache.flink.api.java.ExecutionEnvironment;
import org.apache.flink.api.java.operators.DataSource;
import org.apache.flink.table.api.Table;
import org.apache.flink.table.api.java.BatchTableEnvironment;

import java.util.List;

/**
 * Skeleton for a Flink Batch Job.
 *
 * <p>For a tutorial how to write a Flink batch application, check the
 * tutorials and examples on the <a href="http://flink.apache.org/docs/stable/">Flink Website</a>.
 *
 * <p>To package your application into a JAR file for execution,
 * change the main class in the POM.xml file to this class (simply search for 'mainClass')
 * and run 'mvn clean package' on the command line.
 */
public class BatchJob {

	public static void main(String[] args) throws Exception {
		// set up the batch execution environment
		final ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();

		// Table Environment
		BatchTableEnvironment tableEnvironment = BatchTableEnvironment.getTableEnvironment(env);

		/**
		 * 构造两个数据集，实际生产从自己需要的source中获取即可
		 */
		DataSource<String> dataSourceA_unique = env.fromElements("orderId_1_f113", "orderId_2_f000");
		DataSource<String> dataSourceB_unique = env.fromElements("orderId_2_f000", "orderId_3_f114");


		// 转换成table
		Table tableA_unique = tableEnvironment.fromDataSet(dataSourceA_unique);
		Table tableB_unique = tableEnvironment.fromDataSet(dataSourceB_unique);


		/**
		 * 核心比对（对账）逻辑
		 */
		Table f113_table = tableA_unique.minusAll(tableB_unique);// 差集
		Table f114_table = tableB_unique.minusAll(tableA_unique);// 差集
		Table f000_table = tableA_unique.intersect(tableB_unique);// 交集

		// 转回DataSet用于输出
		DataSet<String> f000 = tableEnvironment.toDataSet(f000_table, String.class);
		DataSet<String> f113 = tableEnvironment.toDataSet(f113_table, String.class);
		DataSet<String> f114 = tableEnvironment.toDataSet(f114_table, String.class);


		/**
		 * 输出，实际输出到自己需要的sink即可
		 */
		List<String> f000_list = f000.collect();
		List<String> f113_list = f113.collect();
		List<String> f114_list = f114.collect();

		System.out.println("==============================");
		System.out.println("f000 ->" + f000_list);
		System.out.println("==============================");
		System.out.println("f113 ->" + f113_list);
		System.out.println("==============================");
		System.out.println("f114 ->" + f114_list);



		// 批处理不需要显示调用execute，否则会报错
		// env.execute("Flink Batch Java API Skeleton");
	}

}
